﻿using Logics.Sys;
using Models.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zo.Models;

namespace BSAppDOne.WebAdmin.Areas.Admin.Controllers.Sys
{
    public class AdminController : BaseController
    {
        AdminLogic _logic = new AdminLogic();
     
        // GET: Admin/Admin
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetData() 
        {
            var data = _logic.GetAllAdmins();
            return Success(data);
        }
        public ActionResult Save(Sys_Admin info,int[] roles)
        {
            var re=_logic.SaveInfo(info, roles);
            if(re)
            {
                return Success();
            }
            return Error("Failed");
        }

        [HttpPost]
        public ActionResult GetAdminInfo(int adminId=0)
        {
            AdminRoleLogic _roleLogic = new AdminRoleLogic();           
            //get admin info
            var admin = _logic.GetAdminInfoById(adminId);
            //get admin role
            var hasRoles=_roleLogic.GetRolesByAdminId(adminId);
            string role_str = string.Empty;
            if(hasRoles!=null&&hasRoles.Count()>0)
            {
                role_str = string.Join(",", hasRoles.Select(c => c.Id));
            }
            return Success(new {info=admin,roles= role_str });
        }

        [HttpPost]
        public ActionResult GetALLRoles()
        {
            RoleLogic _roleLogicAll = new RoleLogic();
            var allRoles = _roleLogicAll.GetAllRoles();
            return Success(allRoles.Select((p, i) => new { id = p.Id, text = p.Name }));
        }

        [HttpPost]
        public ActionResult Delete(int adminId)
        {
            var result=_logic.Delete(adminId);
            if (result)
            {
                return Success();
            }
            return Error();
        }
        [HttpPost]
        public ActionResult ResetPassword(string password,string confirmPassword,int adminId)
        {
            var result=_logic.resetPassword(password, confirmPassword, adminId);
            if (result)
            {
                return Success();
            }
            return Error();
        }
    }
}